#!/usr/bin/env python3.6
import os
import sqlite3


class MovieDB:
    def __init__(self, dbname):
        self.dbname = dbname
        if not os.path.exists(self.dbname):
            conn = sqlite3.connect(self.dbname)
            self.conn = conn
            cursor = conn.cursor()
            self.cursor = cursor
            self.cursor.execute("""create table movieinfo
                                (id integer primary key autoincrement not null,
                                 url text not null,
                                 download_url text not null,
                                 magnetic_link text not null,
                                 year text,
                                 area text,
                                 type text no null,
                                 name text not null)""")

        conn = sqlite3.connect(self.dbname)
        self.conn = conn

        cursor = conn.cursor()
        self.cursor = cursor



    def insert(self, url, download_url,magnetic_link,year, area, type, name):
        self.cursor.execute("""insert into movieinfo(url, download_url,magnetic_link,year, area, type, name) 
                                values ("%s","%s","%s","%s","%s","%s","%s")""" % (
            url, download_url, magnetic_link, year, area, type, name))

    def check_url(self,url):
        self.cursor.execute("select id from movieinfo where url='%s'"%(url))
        data = self.cursor.fetchone()
        if data:
            return data[0]
        else:
            return False
    def getdata(self,id):
        self.cursor.execute("select * from movieinfo where id='%s'"%(id))
        data=self.cursor.fetchone()
        return data


    def browse_data(self,page_num,list_count,):
        """
        浏览数据的分页器
        :param page_num: 浏览的页码
        :param list_count: 每页显示多少条数量
        :return:
        """
        start_id=page_num*list_count-list_count
        end_id=list_count
        self.cursor.execute("select * from (select * from movieinfo order  by id desc) limit '%s','%s'"%(start_id,end_id))
        # self.cursor.execute("select * from movieinfo limit '%s','%s'"%(0,20))
        data=self.cursor.fetchall()

        a = ""
        for item in data:
            a += "{:<8}| {},{},{},{}".format(item[0], '<a href="' + item[1] + '">' + item[4] + '</a>', item[5], item[6],
                                             '<a href="' + item[2] + '">' + item[7] + '</a>' + '\n')
        return a



    def get_magnetic_link(self,id):
        self.cursor.execute("select magnetic_link from movieinfo where id='%s'"%(id))
        magnetic_link=self.cursor.fetchone()
        return magnetic_link[0]

    def commit(self):
        self.conn.commit()

    def close(self):
        self.conn.close()

if __name__ == '__main__':
    movie = MovieDB('moviedb.sqlite')
    # movie = MovieDB('testdb.sqlite')
    # movie = MovieDB('testdb.sqlite')
    # # movie.insert("1","http://www.btbtt.net/attach-download-fid-aid-4662906.htm","http://www.btbtt.net/attach-download-fid-aid-4662906.htm", "2007", "欧美", "动作", "少年泰坦：犹大契约")
    # # movie.insert("2","http://www.btbtt.net/attach-download-fid-aid-4662906.htm","http://www.btbtt.net/attach-download-fid-aid-4662906.htm", "2007", "欧美", "动作", "少年泰坦：犹大契约")
    # # movie.insert("3","http://www.btbtt.net/attach-download-fid-aid-4662906.htm","http://www.btbtt.net/attach-download-fid-aid-4662906.htm", "2007", "欧美", "动作", "少年泰坦：犹大契约")
    # # movie.insert("4","http://www.btbtt.net/attach-download-fid-aid-4662906.htm","http://www.btbtt.net/attach-download-fid-aid-4662906.htm", "2007", "欧美", "动作", "少年泰坦：犹大契约")
    # print(movie.check_url('http://www.btbtt.net/thread-index-fid-951-tid-4464678.htm'))
    # print(movie.check_url("1"))
    # print(movie.get_magnetic_link(1))
    # print(movie.getdata('http://www.btbtt.net/thread-index-fid-951-tid-4464678.htm'))
    # print(movie.getdata(1))
    # movie.commit()
    # movie.close()
    a=movie.browse_data(3,20)
    print(a)
    # for item in a:

        # print(,,i[2],i[3],i[4],i[5],i[6],i[7])
        # print("{:<8}| {},{},{},{}".format(item[0], '<a href="' + item[1] + '">'+item[4]+ '</a>', item[5], item[6],'<a href="' + item[2] + '">' + item[7] + '</a>' + '\n'))
    # print(a)
